/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年6月19日
 * @version 0.1
 */
package com.suredy.base.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.base.model.User2Role;
import com.suredy.core.service.BaseSrvWithEntity;


/**
 * @author ZhangMaoren
 *
 */
@Service("User2RoleSrv")
public class User2RoleSrv extends BaseSrvWithEntity<User2Role>{
	@Override
	public DetachedCriteria getDc(User2Role t) {
		DetachedCriteria dc = super.getDc(t);

		if (t == null) {
			return dc;
		}
		
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		
		if (!StringUtils.isBlank(t.getRoleId())) {
			dc.add(Restrictions.eq("roleId", t.getRoleId()));
		}
		
		if (!StringUtils.isBlank(t.getUserId())) {
			dc.add(Restrictions.eq("userId", t.getUserId()));
		}

		return dc;
	}
	
	public List<User2Role> getAll() {
		return this.readByEntity(null);
	}

	public User2Role get(String id) {
		return this.get(id);
	}
	
	public List<User2Role> getByRole(String roleId) {
		List<User2Role> ret = new ArrayList<User2Role>(); 
		if (StringUtils.isBlank(roleId)) {
			return ret;
		}
		User2Role search = new User2Role();
		search.setRoleId(roleId);
		ret = this.readByEntity(search);
		return ret;
	}
	
	public List<User2Role> getByUser(String userId) {
		List<User2Role> ret = new ArrayList<User2Role>(); 
		if (StringUtils.isBlank(userId)) {
			return ret;
		}
		User2Role search = new User2Role();
		search.setUserId(userId);
		ret = this.readByEntity(search);
		return ret;
	}

}
